package com.huifaapp.mall.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.huifaapp.mall.R;
import com.huifaapp.mall.activity.DailyWillPushActivity;
import com.huifaapp.mall.activity.HtmlActivity;
import com.huifaapp.mall.activity.InvitationVIPActivity;
import com.huifaapp.mall.activity.MinuteKillActivity;
import com.huifaapp.mall.activity.TmallActivity;
import com.huifaapp.mall.activity.WouldCareAuctionActivity;
import com.huifaapp.mall.bean.ModelsBean;
import com.huifaapp.mall.uitils.Contig;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * 品牌列表适配器
 */
public class HomeLivingMoneyTGridViewAdapter extends BaseAdapter{

	private Context context;
	private LayoutInflater inflater;
	private List<ModelsBean.DataBean> serviceareaList;
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	private boolean isChenck=false;
	private ArrayList<String> ar=new ArrayList<String>();
	public HomeLivingMoneyTGridViewAdapter(Context context, List<ModelsBean.DataBean> serviceareaList) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.serviceareaList=serviceareaList;
	}
	@Override
	public int getCount() {
		return serviceareaList == null ? 0 : serviceareaList.size();
	}

	@Override
	public Object getItem(int position) {
		return serviceareaList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public List<ModelsBean.DataBean> getServiceareaList() {
		return serviceareaList;
	}

	public void setServiceareaList(List<ModelsBean.DataBean> serviceareaList) {
		this.serviceareaList = serviceareaList;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		
		if (null == convertView) {
			convertView = inflater.inflate(R.layout.item_home_living_money_t_gridview, null);
			holder = new ViewHolder();
			holder.iv_item_home_living_money_image= (ImageView) convertView.findViewById(R.id.iv_item_home_living_money_image);
			holder.tv_item_home_living_money_imageName= (TextView) convertView.findViewById(R.id.tv_item_home_living_money_imageName);
			holder.ll_living_money_t_gv= (LinearLayout) convertView.findViewById(R.id.ll_living_money_t_gv);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final ModelsBean.DataBean bean=serviceareaList.get(position);
		uploadimageview(holder.iv_item_home_living_money_image,bean.getImg(),null,context);
		holder.tv_item_home_living_money_imageName.setText(bean.getName());

		holder.ll_living_money_t_gv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.e("zhuti","type===="+bean.getValue());
				if (bean.getLinkType().equals("app")) {
					if (bean.getValue().equals("tm")) {//天猫
						Intent intent = new Intent(context, TmallActivity.class);
						intent.putExtra("typex","tm");
						context.startActivity(intent);
					} else if (bean.getValue().equals("jd")) {//京东
						Intent intent = new Intent(context, TmallActivity.class);
						intent.putExtra("typex","jd");
						context.startActivity(intent);

					} else if (bean.getValue().equals("tb")) {//淘宝

					} else if (bean.getValue().equals("yh")) {//友惠
						Intent intent = new Intent(context, TmallActivity.class);
						intent.putExtra("typex","yh");
						context.startActivity(intent);
					} else if (bean.getValue().equals("mrbt")) {//每日必推
						Intent intent = new Intent(context, DailyWillPushActivity.class);
						context.startActivity(intent);
					} else if (bean.getValue().equals("hpm")) {//惠拍卖
						Intent intent = new Intent(context, WouldCareAuctionActivity.class);
						context.startActivity(intent);
					} else if (bean.getValue().equals("exsy")) {//二选手优


					} else if (bean.getValue().equals("inviteVip")) {//邀请领现
						Intent intent = new Intent(context, InvitationVIPActivity.class);
						context.startActivity(intent);
					}else if(bean.getValue().equals("zdtm")){
						Intent intent=new Intent(context, MinuteKillActivity.class);
						Contig.typex="jd";
						context.startActivity(intent);
					}
				}else if(bean.getLinkType().equals("h5")){
					Intent intent=new Intent(context, HtmlActivity.class);
					intent.putExtra("url",bean.getValue());
					context.startActivity(intent);
				}
			}
		});
		return convertView;
	}
	
	
	
	class ViewHolder {
		private ImageView iv_item_home_living_money_image;
		private TextView tv_item_home_living_money_imageName;
		private LinearLayout ll_living_money_t_gv;
	}

	public static void uploadimageview(final ImageView views, String url, String bili, Context c) {
		DisplayImageOptions HXOptions = new DisplayImageOptions.Builder()
				.cacheInMemory(true)
				.cacheOnDisk(true)
				.considerExifParams(true)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
		ImageLoader.getInstance().displayImage(url, views, HXOptions);
	}
}
